package com.flyco.modules.stm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.oms.entity.OmsOrderPriceDistance;
import com.flyco.modules.oms.service.IOmsOrderPriceDistanceService;
import com.flyco.modules.stm.entity.StmChannelInfo;
import com.flyco.modules.stm.mapper.StmChannelInfoMapper;
import com.flyco.modules.stm.service.IStmChannelInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 渠道表
 * @Author: flyco
 * @Date: 2023-06-21
 * @Version: V1.0
 */
@Service
public class StmChannelInfoServiceImpl extends ServiceImpl<StmChannelInfoMapper, StmChannelInfo> implements IStmChannelInfoService {

    @Autowired
    private StmChannelInfoMapper channelInfoMapper;
    @Autowired
    private IOmsOrderPriceDistanceService distanceService;

    @Override
    public List<StmChannelInfo> getByChannelIds(List<String> channelList) {

        LambdaQueryWrapper<StmChannelInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(StmChannelInfo::getChannelId, channelList);
        return channelInfoMapper.selectList(wrapper);
    }

    @Override
    public List<StmChannelInfo> queryChannelListAndOrderDistance() {

        LambdaQueryWrapper<StmChannelInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StmChannelInfo::getStatus, 1);
        List<StmChannelInfo> channelInfoList = list(wrapper);

        List<OmsOrderPriceDistance> distanceList = distanceService.list().stream().filter(o -> o.getChannelId()!=null).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(distanceList)) {
            Map<Integer, List<OmsOrderPriceDistance>> distanceMap = distanceList.stream()
                    .collect(Collectors.groupingBy(OmsOrderPriceDistance::getChannelId));

            channelInfoList.forEach(o -> {
                List<OmsOrderPriceDistance> distances = distanceMap.get(o.getId());
                if (!CollectionUtils.isEmpty(distances)) {
                    o.setName(o.getName() + "(已配置)");
                }
            });
        }
        ;
        return channelInfoList;
    }

    @Override
    public List<StmChannelInfo> getChannelList(){
        LambdaQueryWrapper<StmChannelInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StmChannelInfo::getStatus,1);
        return list(wrapper);
    }
}
